Use case: PWM generator with external clock

Sequence diagram

LPTIM PWM generator with external clock

  @startuml
  title: LPTIM PWM Generator with external clock

  participant "User Application" as appli
  participant "<font color=green><b>HAL LPTIM</b></font>" as LPTIM

  group#Bisque #Linen Generating PWM signal

  == Prerequisite: initialization and configuration  ==
  appli->LPTIM : HAL_LPTIM_Init()\n
  LPTIM-->appli : <color #Green> HAL_OK

  == PWM Generating Process ==
  appli->LPTIM : HAL_LPTIM_OC_ConfigChannel()
  LPTIM-->appli : <color #Green> HAL_OK
  appli->LPTIM : HAL_LPTIM_PWM_Start();
  LPTIM-->appli : <color #Green> HAL_OK
  hnote across: Enter Low Power Mode
  appli->LPTIM : HAL_LPTIM_PWM_Stop()
  LPTIM-->appli : <color #Green> HAL_OK
  == LPTIM Deinitialization ==
  appli->LPTIM : <b>HAL_LPTIM_DeInit</b>()
  LPTIM-->appli : <color #Green> HAL_OK
  end

  @enduml

Code snippet

HAL1

HAL2

LPTIM_HandleTypeDef             LptimHandle;

#define PeriodValue             (uint32_t) (100 -1)
#define PulseValue              (uint32_t) (50 -1)

LptimHandle.Instance = LPTIM1;

LptimHandle.Init.Clock.Source    = LPTIM_CLOCKSOURCE_ULPTIM;
LptimHandle.Init.CounterSource   = LPTIM_COUNTERSOURCE_EXTERNAL;
LptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
LptimHandle.Init.Trigger.Source  = LPTIM_TRIGSOURCE_SOFTWARE;
LptimHandle.Init.OutputPolarity  = LPTIM_OUTPUTPOLARITY_HIGH;
LptimHandle.Init.UpdateMode      = LPTIM_UPDATE_IMMEDIATE;














/* Initialize LPTIM peripheral according to the passed parameters */
HAL_LPTIM_Init(&LptimHandle);


/* Start generating the PWM signal */





HAL_LPTIM_PWM_Start(&LptimHandle, LPTIM_CHANNEL_1);

/* Enter in Stop mode */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

/* Stop PWM when done and De-Initialize the LPTIM Instance*/
HAL_LPTIM_PWM_Stop(&LptimHandle);
HAL_LPTIM_DeInit(&LPTIM_Handle);
hal_lptim_handle_t LPTIM_Handle;
hal_lptim_config_t p_config;
hal_lptim_input1_config_t in1Config;
hal_lptim_oc_config_t sConfig;



p_config.clock_source = HAL_LPTIM_CLK_EXTERNAL_ASYNCHRONOUS;

p_config.prescaler = HAL_LPTIM_CLK_SRC_DIV1;
p_config.mode = HAL_LPTIM_CONTINUOUS; /*To generate a PWM signal*/
p_config.repetition_counter = 0;
p_config.period = 99;

/* Set Input1 config to be used as an external clock for the LPTIM instance */
in1Config.src = HAL_LPTIM_INPUT1_GPIO;
in1Config.filter = HAL_LPTIM_FDIV1;
in1Config.polarity = HAL_LPTIM_INPUT1_RISING;





/* Initialize and configure the LPTIM Instance */
HAL_LPTIM_Init(&LPTIM_Handle, HAL_LPTIM1);
HAL_LPTIM_SetConfig(&LPTIM_Handle, &p_config);

/* Start generating the PWM signal */
HAL_LPTIM_SetConfigInput1(&LPTIM_Handle, &in1Config);

HAL_LPTIM_OC_SetConfigChannel(&LPTIM_Handle, HAL_LPTIM_CHANNEL_1, sConfig);

HAL_LPTIM_OC_StartChannel(&LPTIM_Handle, channel);

HAL_LPTIM_Start(&LPTIM_Handle);

/* Enter in Stop mode */
HAL_PWR_EnterStopMode(HAL_PWR_LOW_PWR_MODE_WFE, HAL_PWR_STOP1_MODE);

/* Stop PWM when done and De-Initialize the LPTIM Instance*/
HAL_LPTIM_Stop(&LPTIM_Handle);
HAL_LPTIM_DeInit(&LPTIM_Handle);

Key changes

  1. HAL2 splits configuration from initialization using HAL_LPTIM_SetConfig().

  2. HAL2 configures external clock input explicitly using HAL_LPTIM_SetConfigInput1().

  3. HAL2 starts and stops the channel separately from the peripheral using HAL_LPTIM_OC_StartChannel() / HAL_LPTIM_StopChannel() and HAL_LPTIM_Start() / HAL_LPTIM_Stop().